using System;
using System.Collections.Generic;
using System.Threading;
using ByteDance.Union;
using ByteDance.Union.Mediation;
using UnityEngine;

/**
 * 激励视频代码示例。
 * 注：该接口支持融合功能
 */
public class ExampleRewardAd
{

    // 加载广告
    public static void LoadReward()
    {
        // 释放上一次广告
        AdvCtrlManager.Instance().OnDisposeReward();

        // 竖屏
        // 创造广告参数对象
        var adSlot = new AdSlot.Builder()
            .SetCodeId(AdvCtrlManager.M_REWARD_VIDEO_V_ID) // 必传
            .SetUserID(UserInfoConfig.Instance().UserID) // 用户id,必传参数
            .SetOrientation(AdOrientation.Vertical) // 必填参数，期望视频的播放方向
            .SetRewardName("银币") // 可选
            .SetRewardAmount(777) // 可选
            .SetMediaExtra("media_extra") //⚠️设置透传信息(穿山甲广告 或 聚合维度iOS广告时)，需可序列化
            .SetMediationAdSlot(
                new MediationAdSlot.Builder()
#if UNITY_ANDROID  //⚠️设置透传信息(当加载聚合维度Android广告时)
                    .SetExtraObject(AdConst.KEY_GROMORE_EXTRA, "gromore-server-reward-extra-unity") // 可选，设置gromore服务端验证的透传参数
                    .SetExtraObject("pangle", "pangleCustomData") // 可选，不是gromore服务端验证时，用于各个adn的参数透传
#endif
                    .SetScenarioId("reward-m-scenarioId") // 可选
                    .SetBidNotify(true) // 可选
                    .SetUseSurfaceView(false) // 可选
                    .Build()
                    )
            
            .Build();
        // 加载广告
        SDK.CreateAdNative().LoadRewardVideoAd(adSlot, new RewardVideoAdListener());
    }

    // 展示广告
    public static void ShowReward()
    {
        var rewardad = AdvCtrlManager.Instance().OnGetReward();
        if (rewardad == null)
        {
            Debug.LogError("CSJM_Unity " + "请先加载广告");
        }
        else
        {
            // 设置展示阶段的监听器
            rewardad.SetRewardAdInteractionListener(new RewardAdInteractionListener());
            rewardad.SetAgainRewardAdInteractionListener(new RewardAgainAdInteractionListener());
            rewardad.SetDownloadListener(new AppDownloadListener());
            rewardad.SetAdInteractionListener(new TTAdInteractionListener());
#if UNITY_ANDROID
            rewardad.SetRewardPlayAgainController(new RewardAdPlayAgainController());
#endif
            rewardad.ShowRewardVideoAd();
        }
    }

    /**
     * 广告加载监听器
     */
    public sealed class RewardVideoAdListener : IRewardVideoAdListener
    {
        public RewardVideoAdListener()
        {
        }

        public void OnError(int code, string message)
        {
            Debug.LogError("CSJM_Unity " + $"OnRewardError:{message} on main thread:");

        }

        public void OnRewardVideoAdLoad(RewardVideoAd ad)
        {
            Debug.Log("CSJM_Unity " + $"OnRewardVideoAdLoad on main thread: ");
            
            AdvCtrlManager.Instance().OnGetReward(ad);
            ShowReward();
        }

        public void OnRewardVideoCached()
        {
            Debug.Log("CSJM_Unity " + $"OnRewardVideoCached on main thread: ");
        }

        public void OnRewardVideoCached(RewardVideoAd ad)
        {
            Debug.Log("CSJM_Unity " + $"OnRewardVideoCached RewardVideoAd ad on main thread: ");
        }
    }

    // 广告展示监听器
    public sealed class RewardAdInteractionListener : IRewardAdInteractionListener
    {
        public RewardAdInteractionListener()
        {
        }

        public void OnAdShow()
        {
            Debug.Log("CSJM_Unity " + $"rewardVideoAd show on main thread: ");
        }

        public void OnAdVideoBarClick()
        {
            Debug.Log("CSJM_Unity " + $"rewardVideoAd bar click on main thread: ");
        }

        public void OnAdClose()
        {
            Debug.Log("CSJM_Unity " + $"rewardVideoAd close on main thread: ");
            
            AdvCtrlManager.Instance().OnDisposeReward();
        }

        public void OnVideoSkip()
        {
            Debug.Log("CSJM_Unity " + $"rewardVideoAd skip on main thread: ");
        }

        public void OnVideoComplete()
        {
            Debug.Log("CSJM_Unity " + $"rewardVideoAd complete on main thread: ");
            
            AdvCtrlManager.Instance().OnSeeAdvCallback();
        }

        public void OnVideoError()
        {
            Debug.LogError("CSJM_Unity " + $"rewardVideoAd error on main thread: ");
        }

        public void OnRewardArrived(bool isRewardValid, int rewardType, IRewardBundleModel extraInfo)
        {
            var logString = "OnRewardArrived verify:" + isRewardValid + " rewardType:" + rewardType + " extraInfo: " + extraInfo.ToString() +
                            $" on main thread: ";
            Debug.Log("CSJM_Unity " + logString);
            if (extraInfo.ServerErrorCode == 0)//表示广告看完了，给奖励
            {
                AdvCtrlManager.Instance().OnSeeAdvCallback();
            }
        }
    }

    // 广告再看一个监听器
    public sealed class RewardAdPlayAgainController : IRewardAdPlayAgainController
    {
        public void GetPlayAgainCondition(int nextPlayAgainCount, Action<PlayAgainCallbackBean> callback)
        {
            Debug.Log("CSJM_Unity " + "Reward GetPlayAgainCondition");
            var bean = new PlayAgainCallbackBean(true, "金币", nextPlayAgainCount + "个");
            callback?.Invoke(bean);
        }
    }

    // 广告再看一个监听器
    public sealed class RewardAgainAdInteractionListener : IRewardAdInteractionListener
    {
        public RewardAgainAdInteractionListener()
        {
        }

        public void OnAdShow()
        {
            Debug.Log("CSJM_Unity " + $"again rewardVideoAd show on main thread: ");
        }

        public void OnAdVideoBarClick()
        {
            Debug.Log("CSJM_Unity " + $"again rewardVideoAd bar click on main thread: ");
        }

        public void OnAdClose()
        {
            Debug.Log("CSJM_Unity " + "OnAdClose");
        }

        public void OnVideoSkip()
        {
            Debug.Log("CSJM_Unity " + $"again rewardVideoAd skip on main thread: ");
 
        }

        public void OnVideoComplete()
        {
            Debug.Log("CSJM_Unity " + $"again rewardVideoAd complete on main thread: ");
            
            AdvCtrlManager.Instance().OnSeeAdvCallback();
        }

        public void OnVideoError()
        {
            Debug.LogError("CSJM_Unity " + $"again rewardVideoAd error on main thread: ");
        }

        public void OnRewardArrived(bool isRewardValid, int rewardType, IRewardBundleModel extraInfo)
        {
            var logString = "again OnRewardArrived verify:" + isRewardValid + " rewardType:" + rewardType + " extraInfo:" + extraInfo +
                            $" on main thread: ";
            Debug.Log("CSJM_Unity " + logString);
        }
    }
}
